package com.situ.day1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
 
    public class JDBCUtilEnd {
         private static  String driver;   //mysql的路径
         private static  String url ;     //数据库路径
         private static  String user ;    //账号
         private static  String password; //密码
         
         static {
             InputStream inputStream;
             try {
                 inputStream = new FileInputStream("src2/com/situ/day1/db.properties");// I/O流打开文件所在的路径
                 Properties properties = new Properties();            //new properties
                 properties.load(inputStream);                        //读文件
                 driver = properties.getProperty("driver");           //写入关联文件
                 url = properties.getProperty("url");
                 user = properties.getProperty("user");
                 password = properties.getProperty("password");
                 properties.getProperty("password");
             } catch (FileNotFoundException e1) {
                 e1.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }
                  
             
             try {                                            
                 //1、加载驱动 Class.forName("");
                 Class.forName(driver);         //加载mysql的路径
             } catch (ClassNotFoundException e) {
                 e.printStackTrace();
             }
         }
         
         //2、获得连接对象Connection
         public static Connection getConnection() throws SQLException {  // 获得对象  -- 抛出的异常
             Connection connection = DriverManager.getConnection(url, user, password);  // 数据库路径、账号、密码
             return connection;                                           //返回connection
         }
         
         //6、关闭连接
         public static void close(Connection connection, Statement statement, ResultSet resultSet) { 
             if (resultSet != null) {        //关闭resultSet、statement、connection
                 try {
                     resultSet.close();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }
             }
             if (statement != null) {
                 try {
                     statement.close();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }
             }
             if (connection != null) {
                 try {
                     connection.close();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }
             }
         }
    }